package com.wxq.draw;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wxq.shape.MapShape;
import com.wxq.shape.MapText;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Paint.Style;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class DrawView extends View {

	public static final String Tag = "DrawView";
	public static final String SHAPE_KEY = "shape";
	public static final String PAINT_FILL_KEY = "paint_fill";
	public static final String PAINT_STROKE_KEY = "paint_stroke";
	public static final String RENDER_STYLE_KEY = "render_style";
	public static final String RENDER_KEY = "render_key";
	public static final int RENDER_STYLE_STROKE = 0;
	public static final int RENDER_STYLE_FILL = 1;
	public static final int RENDER_STYLE_BOTH = 2;
	public static final String VISIBLE_KEY = "Visible_key";
	public static final int VISIBLE_YES = 1;
	public static final int VISIBLE_NO = 0;
	public static final String SPOT_ID_KEY = "spot_id_key";
	public static final String PATH_KEY = "path_key";
	public static final String TEXT_KEY = "textStr_key";
	public static final String SPOTINFO_ID_KEY = "spotId";
	public static final String SPOTINFO_NAME_KEY = "spotName";
	
//	public static final int  
	
	//--------------------位移偏移数据----------------------
	
	//先缩放,后平移，当计算原图坐标时，先把坐标平移回原点，再恢复缩放比例。
	//总体偏移
	public float xOffset = 0.0f;
	public float yOffset = 0.0f;
	
	//-----------------------------------缩放使用的数据-----------------------
	Matrix scaleMatrix = new Matrix();
	//缩放比例范围
	static float [] scaleArray = {0.7f,1.0f,1.5f,2.0f,2.5f,3.0f,4.0f,5.5f,7.0f,9.0f,12.0f,16.0f,20.0f};
	//缩放游标
	int scaleIndex = 4;
	int scalemin = 0;
	int scalemax = scaleArray.length;
	//缩放比例 初始
	public float scale = scaleArray[scaleIndex];
	
	//-------------------------地图数据------------------------
	//绘图元素列表
	public List< Map<String, Object> > shapeList;
	//地图数据加载工具类
	DrawViewDataHelper helper;
	
	//默认绘图画笔
	Paint defaultStrokePaint;
	Paint defaultFillPaint;
	
	{
		defaultFillPaint = new Paint();
		defaultFillPaint.setAntiAlias(true);    //设置无锯齿
		defaultFillPaint.setStyle(Style.FILL);  //设置填充方式
		defaultFillPaint.setColor(Color.RED);   //设置颜色
		
		defaultStrokePaint = new Paint();
		defaultStrokePaint.setAntiAlias(true);    //设置无锯齿
		defaultStrokePaint.setStyle(Style.STROKE);  //设置填充方式
		defaultStrokePaint.setColor(Color.YELLOW);
		defaultStrokePaint.setStrokeWidth(2.0f);//线宽
	}

	public DrawView(Context context) {
		super(context);
	}

	public DrawView(Context context, AttributeSet attrs) {
		super(context, attrs);		
	}
	
	public void loadData(String mapId) {	
		if(helper!=null)
			helper.close();
		helper = new DrawViewDataHelper(getContext(), mapId);
		this.shapeList = helper.getShapeList();
		scaleIndex=3;
		setScaleRange(0,7);
		setScale(scaleIndex);//设置初始缩放比例
		invalidate();
	}
	
	public void changedbmap(String filename,String floor){
		//if(helper==null)
		if(helper!=null)
			helper.close();
		helper = new DrawViewDataHelper(getContext(), floor,filename);
		this.shapeList = helper.getShapeList();
		if(filename.equals("phone3")){
		  scaleIndex=3;
		  setScaleRange(0,6);
		}else{
		scaleIndex=8;
		setScaleRange(5,scaleArray.length);
		}
		setScale(scaleIndex);//设置初始缩放比例
		invalidate();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		测试代码！！！
//		Path testPath = new Path();```
//		testPath.addCircle(100, 100, 100, Direction.CW);
//		MapPath testMapPath = new MapPath(testPath);
//		testMapPath.draw(canvas, defaultFillPaint);
//		canvas.drawPath(testPath, defaultFillPaint);
		
		if (shapeList == null) {
			Log.d("wxq", "没有绘图对象！");
			return;
		}
		
		for(int i = 0 ;i <shapeList.size();i++)
		{
			Map<String, Object> m = shapeList.get(i);
			
			//轮廓位移
			MapShape ms = (MapShape) m.get(SHAPE_KEY);
			ms.offset(xOffset, yOffset);
			
			//获得是否显示
			String visibleStr = (String) m.get(VISIBLE_KEY);
			int visible = Integer.parseInt(visibleStr);
			if (visible == VISIBLE_NO) {
				//Log.d("wxq", "此对象无需显示！");
				continue;
			}
			
			//获得render信息
			@SuppressWarnings("unchecked")
			Map<String, Object> item = (Map<String, Object>) m.get(RENDER_KEY);
			String styleStr = (String) item.get(RENDER_STYLE_KEY);
			int style = Integer.parseInt(styleStr);
						
			//-----------------绘制------------------------
			if (!(ms instanceof MapText)) {
				//画非文字
				if (style == RENDER_STYLE_STROKE) {//stroke
					Paint strokePaint = (Paint) item.get(PAINT_STROKE_KEY);
					if (strokePaint == null) {
						strokePaint = defaultStrokePaint;
					}
					ms.draw(canvas, strokePaint);
				}
				else if(style == RENDER_STYLE_FILL){//fill
					Paint fillPaint = (Paint) item.get(PAINT_FILL_KEY);
					if (fillPaint == null) {
						fillPaint = defaultFillPaint;
					}
					ms.draw(canvas, fillPaint);
				}
				else if (style == RENDER_STYLE_BOTH) {//both
					//先画填充
					Paint fillPaint = (Paint) item.get(PAINT_FILL_KEY);
					if (fillPaint == null) {
						fillPaint = defaultFillPaint;
					}
					ms.draw(canvas, fillPaint);
					//后画边框
					Paint strokePaint = (Paint) item.get(PAINT_STROKE_KEY);
					if(strokePaint == null)
					{
						strokePaint = defaultStrokePaint;
					}
					ms.draw(canvas, strokePaint);
				}
			}
			else {
				//画文字
				Paint fillPaint = (Paint) item.get(PAINT_FILL_KEY);
				//if(scaleIndex>2)
				ms.draw(canvas, fillPaint);
			}
		}		
	}
	
	//----------------缩放相关，每个图元都自己缩放一次-------------------------------------------------
	private void zoom(float scale) {
		if (shapeList == null) {
			return;
		}
		
		this.scale = scale;
		Log.d(Tag, "zoom for size:"+this.scale);
		
		scaleMatrix = new Matrix();
		scaleMatrix.setScale(this.scale, this.scale);
		
		for (Map<String, Object> m : shapeList) {
			MapShape ms = (MapShape) m.get(SHAPE_KEY);
			ms.zoom(scaleMatrix);
		}
		invalidate();
	}
	
	public void zoomIn() {
		int tempScaleIndex = scaleIndex - 1;
		setScale(tempScaleIndex);
	}
	
	public void zoomOut() {
		int tempScaleIndex = scaleIndex + 1;
		setScale(tempScaleIndex);
	}
	
	public void setScale(int index) {
		if(index >= scalemin && index < scalemax)
		{
			scaleIndex = index;
			zoom(scaleArray[scaleIndex]);
		}
	}
	
	public void setScaleRange(int min,int max) {
		scalemin=min;
		scalemax=max;
	}
	//----------------位移相关-------------------------------------------------
	 
	public void scrollByDistance(float distanceX, float distanceY) {
		xOffset = (float) (xOffset-distanceX);
		yOffset = (float) (yOffset-distanceY);
		invalidate();
	}
		//---------------------------------------------------------------
	 public void close() {
		 if (helper!=null) {
			 helper.close();
		}
		 
	}
	 
	//---------------判断某点是否在某个商铺中------------------
	public Map<String, String> getSpotNameAtPoint(PointF pointF) {
		Map<String, String> spotInfo = new HashMap<String, String>();
		String spotId = PolygonTool.getClickedShopInMap(shapeList, pointF);
		String spotName = "";
		if (TextUtils.isEmpty(spotId)) {
			Log.d("wxq", "未找到对应的商铺ID！");
			spotId = "";
		}
		else {
			spotName = helper.getSpotName(spotId);
		}
		
		spotInfo.put(SPOTINFO_ID_KEY, spotId);
		spotInfo.put(SPOTINFO_NAME_KEY, spotName);
		
		return spotInfo;
	}
	
}
